<template>
  <div class="app-container">
    <main-panel>
      <div class="right">
        <el-button size="small"
                   icon="el-icon-printer"
                   @click.native.prevent="">打印</el-button>
        <el-button size="small"
                   type="success"
                   icon="el-icon-upload"
                   @click.native.prevent="">同步服务器数据</el-button>
      </div>
    </main-panel>
    <div class="source">
      <el-table :data="list"
                v-loading.body="listLoading"
                element-loading-text="Loading"
                highlight-current-row
                size="small"
                style="width: 100%"
                max-height="450"
                :default-sort="{prop: 'esa_code'}">
        <el-table-column type="selection"
                         width="55">
        </el-table-column>
        <el-table-column label="ESA编码"
                         sortable
                         prop="esa_code">
          <template slot-scope="scope">
            <el-input v-if="editId === scope.row.id"
                      size="mini"
                      v-model="scope.row.esa_code"
                      placeholder="请输入内容"
                      @change="handleEdit(scope.$index, scope.row)"></el-input>
            <span v-else>{{scope.row.esa_code}}</span>
          </template>
        </el-table-column>
        <el-table-column label="ESA名称">
          <template slot-scope="scope">
            <el-input v-if="editId === scope.row.id"
                      size="mini"
                      v-model="scope.row.esa_name"
                      placeholder="请输入内容"
                      @change="handleEdit(scope.$index, scope.row)"></el-input>
            <span v-else>{{scope.row.esa_name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="CSA编码"
                         sortable
                         prop="csa_code">
          <template slot-scope="scope">
            <el-input v-if="editId === scope.row.id"
                      size="mini"
                      v-model="scope.row.csa_code"
                      placeholder="请输入内容"
                      @change="handleEdit(scope.$index, scope.row)"></el-input>
            <span v-else>{{scope.row.csa_code}}</span>
          </template>
        </el-table-column>
        <el-table-column label="CSA名称">
          <template slot-scope="scope">
            <el-input v-if="editId === scope.row.id"
                      size="mini"
                      v-model="scope.row.csa_name"
                      placeholder="请输入内容"
                      @change="handleEdit(scope.$index, scope.row)"></el-input>
            <span v-else>{{scope.row.csa_name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         align="center"
                         width="150">
          <template slot-scope="scope">
            <el-button v-if="editId === scope.row.id"
                       size="mini"
                       type="success"
                       :loading="saveLoading"
                       @click.native.prevent="saveEdit(scope.$index, scope.row)">保存</el-button>
            <el-button v-else
                       size="mini"
                       type="primary"
                       @click.native.prevent="handleEdit(scope.$index, scope.row)">修改</el-button>
          </template>

        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { MainPanel } from '@/components'
import { getList } from '@/api/supplies'
export default {
  name: 'supplies',
  data() {
    return {
      editId: -1,
      listLoading: true,
      saveLoading: false,
      list: []
    }
  },
  created() {
    this.fecthData()
  },
  components: {
    MainPanel
  },
  methods: {
    fecthData() {
      getList().then(res => {
        console.log(res)
        this.list = res.data
        this.listLoading = false
      })
    },
    handleEdit(index, row) {
      if (this.editId !== -1) {
        this.$message.error('有数据正在修改，请先保存')
      } else {
        this.editId = row.id
        console.log(index, row)
      }
    },
    saveEdit(index, row) {
      this.editId = -1
      this.saveLoading = true
      getList().then(res => {
        console.log(res)
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.saveLoading = false
        this.editId = -1
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.source {
  padding: 24px;
  border: 1px solid #ebebeb;
  border-radius: 3px;
  transition: 0.2s;
  margin-top: 24px;
}
.table {
  width: 100%;
}
// 滚动条的宽度
/deep/ .el-table__body-wrapper::-webkit-scrollbar {
  width: 6px; // 横向滚动条
  height: 6px; // 纵向滚动条
}
// 滚动条的滑块
/deep/ .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #ddd;
  border-radius: 3px;
}
.search-form {
  width: 100%;
  .el-input {
    width: 80%;
  }
  .el-select {
    width: 80%;
    .el-option {
      width: 100%;
    }
  }
  .search {
    width: 100%;
  }
  .line {
    text-align: center;
  }
}
.right {
  text-align: right;
}
</style>